var quotePrintFactoryVm = new Vue({
	el:'#quotePrintFactoryPage',
	data:{
	    id:'',
		title: null,
        updateVisible:false,
        quotePrintFactory:{},
		/*初始化包装类*/
        quotePrintFactoryVo:{quotePrintFactory: {canUv:false},quotePrintFacotoryPirceList:[{begin_price:'',step_price:'',display:true}],uVQuotePrintFacotoryPirceList:[]},
        /* 查询 */
        q:{
            printFactoryName: '',
        },
        e: {
            /* 工厂打印机机器类型 */
            quotePrintFactoryEnum:[],
            /* 工厂打印机打印类型 */
            quotePrintPrintTypeEnum:[],
            /* 工厂打印机意颜色类型 */
            quotePrintFactoryPriceColourEnum:[],
        },
        /* 接收查询结果 */
        l: {
            quotePrintList: [],
        },
        //是否按价格系数配置特种纸变价
        followUv:false,
        //特种纸张数据
        uVQuotePrintFacotoryPirceList:[],
	},
    watch:{
	  'quotePrintFactoryVo.quotePrintFacotoryPirceList':{
          handler:function(newValue,oldValue){
              if(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.canUv&&quotePrintFactoryVm.followUv){
                  quotePrintFactoryVm.copyPrice();
              }
          },
          deep:true,
      }
    },
	methods: {
	    loadData:function(data){
            quotePrintFactoryVm.id=data.id;
            quotePrintFactoryVm.title='配价';
            quotePrintFactoryVm.e.quotePrintFactoryPriceColourEnum=vm.e.quotePrintFactoryPriceColourEnum;
            quotePrintFactoryVm.l.quotePrintList=vm.l.quotePrintList ;
            quotePrintFactoryVm.e.quotePrintFactoryEnum=vm.e.quotePrintFactoryEnum
            quotePrintFactoryVm.e.quotePrintPrintTypeEnum=vm.e.quotePrintPrintTypeEnum
            quotePrintFactoryVm.e.quotePrintFactoryPriceColourEnum=vm.e.quotePrintFactoryPriceColourEnum
            quotePrintFactoryVm.getInfo(quotePrintFactoryVm.id);
        },
		update: function (event) {
            //存放特种纸张价格
            if(!quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.canUv){
                quotePrintFactoryVm.uVQuotePrintFacotoryPirceList=[];
            }
            if(!quotePrintFactoryVm.followUv){
                quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio=0;
            }
            quotePrintFactoryVm.quotePrintFactoryVo.uVQuotePrintFacotoryPirceList=quotePrintFactoryVm.uVQuotePrintFacotoryPirceList;
            var url = "quotePrintFactory/quoteprintfactory/updateWithPrice";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(quotePrintFactoryVm.quotePrintFactoryVo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            quotePrintFactoryVm.updateVisible=false;
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
        //校验参数
        factoryValidate:function(factory){
            if(isBlank(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.minWeight)){
                alert("请输入最小克重!");
                return false;
            }
            if(isBlank(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.maxWeight)){
                alert("请输入最大克重!");
                return false;
            }
            return true;
        },
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "quotePrintFactory/quoteprintfactory/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
            quotePrintFactoryVm.uVQuotePrintFacotoryPirceList=[];
			$.get(baseURL + "quotePrintFactory/quoteprintfactory/info/"+id, function(r){
			    if(r.code==0){
                    //获取包装类的
                    quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory=r.quotePrintFactoryVo.quotePrintFactory;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList=r.quotePrintFactoryVo.uVQuotePrintFacotoryPirceList;
                    quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList=r.quotePrintFactoryVo.quotePrintFacotoryPirceList;
                    if( quotePrintFactoryVm.uVQuotePrintFacotoryPirceList.length>0){
                        quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.canUv=true;
                    }else{
                        quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.canUv=false;
                    }
                    if(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio>0){
                        quotePrintFactoryVm.followUv=true;
                    }
                    quotePrintFactoryVm.updateVisible=true;
                }else{
			        alert(r.msg);
                }
            });
		},
		reload: function (event) {
            quotePrintFactoryVm.updateVisible=false;
		},
        deleteRow(index, rows) {//删除改行
            rows.splice(index, 1);
            quotePrintFactoryVm.uVQuotePrintFacotoryPirceList.splice(index, 1);
        },
        addRow(tableData,event){
            tableData.push({colour:'',beginPrice:0,stepPrice:0,largePrice:0,riseBeginPrice:0,riseStepPrice:0,riseLarge:0,printPlatePrice:0,floatingCoefficient:0,display:true});
        },
        //修改是否显示状态
        updatedisplay(id){
            var url ="quotePrintFactory/quoteprintfactory/updateDisplay"+id;
            $.get(baseURL + "quotePrintFactory/quoteprintfactory/updateDisplay/"+id, function(r){
                if(r.code == 0){
                    alert('操作成功', function(index){
                        $("#jqGrid").trigger("reloadGrid");
                    });
                }else{
                    alert(r.msg);
                }
            });
        },
        //开启特种纸价格设置
        changeUv:function () {
	        if(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.canUv){
                quotePrintFactoryVm.copyPrice();
            }else{
                quotePrintFactoryVm.followUv=false;
                quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio=0;
            }
        },
        copyPrice:function() {
	        if(quotePrintFactoryVm.uVQuotePrintFacotoryPirceList.length<quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList.length){
	            var length=quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList.length-quotePrintFactoryVm.uVQuotePrintFacotoryPirceList.length;
	            for(var  i=0;i<length;i++){
                    var item={colour:'',beginPrice:0,stepPrice:0,largePrice:0,riseBeginPrice:0,riseStepPrice:0,riseLarge:0,printPlatePrice:0,floatingCoefficient:0,display:true};
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList.push(item);
                }
            }
            if(quotePrintFactoryVm.followUv){
                if(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio==0&&isBlank(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio)){
                    quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio=1;
                }
                for (var i = 0; i < quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList.length; i++) {
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].colour = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].colour;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].beginPrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].beginPrice * quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].stepPrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].stepPrice * quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].largePrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].largePrice * quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].riseBeginPrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseBeginPrice * quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].riseStepPrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseStepPrice * quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].riseLarge = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseLarge * quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].printPlatePrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].printPlatePrice * quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].floatingCoefficient = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].floatingCoefficient;
                }
            }else{
                for (var i = 0; i < quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList.length; i++) {
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].colour = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].colour;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].beginPrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].beginPrice ;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].stepPrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].stepPrice;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].largePrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].largePrice;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].riseBeginPrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseBeginPrice ;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].riseStepPrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseStepPrice ;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].riseLarge = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseLarge;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].printPlatePrice = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].printPlatePrice;
                    quotePrintFactoryVm.uVQuotePrintFacotoryPirceList[i].floatingCoefficient = quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].floatingCoefficient;
                }
            }
        } ,

        //是否按照特种纸张变价
        changeFollowUv:function () {
            if(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio==0||isBlank(quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio)){
                quotePrintFactoryVm.quotePrintFactoryVo.quotePrintFactory.ratio=1;
                quotePrintFactoryVm.copyPrice();
             }
        },
        //改变变价系数
        changeRatio:function(){
            quotePrintFactoryVm.copyPrice();
        },
        //
        colorFormat:function (row) {
		    var colour=row.colour;
           for (var i=0;i<quotePrintFactoryVm.e.quotePrintFactoryPriceColourEnum.length;i++){
               if(colour==quotePrintFactoryVm.e.quotePrintFactoryPriceColourEnum[i].value){
                   return quotePrintFactoryVm.e.quotePrintFactoryPriceColourEnum[i].describe;
               }
           }
        }
	}
});